/*
 * Eugen Stoian <stoian.e@gmail.com> 2010
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "PlotableObject3D.h"
#include <Real.h>
#include <cmath>

PlotableObject3D::PlotableObject3D(QObject * parent)
        :Object(parent),_id(0),
        _xMin(Real::positiveInfinity()),
        _yMin(Real::positiveInfinity()),
        _zMin(Real::positiveInfinity()),
        _xMax(Real::negativeInfinity()),
        _yMax(Real::negativeInfinity()),
        _zMax(Real::negativeInfinity())
{
    _color = QColor(Qt::red);
    //connect(this,SIGNAL(changed()),this,SLOT(reset()));
    //connect(this,SIGNAL(changed()),this,SLOT(compute()));
}

void PlotableObject3D::display() {
    if(!id()) {
        compile();
    }
    glCallList(id());
}
GLuint PlotableObject3D::id() const {
    return _id;
}
void PlotableObject3D::compile() {
    _id = glGenLists(1);
    glNewList(_id,GL_COMPILE);
    draw();
    glEndList();
}

QColor PlotableObject3D::color() const {
    return _color;
}

double PlotableObject3D::xMin() const{
    return _xMin;
}

double PlotableObject3D::yMin() const{
    return _yMin;
}

double PlotableObject3D::zMin() const{
    return _zMin;
}

double PlotableObject3D::xMax() const{
    return _xMax;
}

double PlotableObject3D::yMax() const{
    return _yMax;
}

double PlotableObject3D::zMax() const{
    return _zMax;
}


double PlotableObject3D::radius() const{
    if(!isValid())
        return 0.0;
    double xr = _xMax - _xMin;
    double yr = _yMax - _yMin;
    double zr = _zMax - _zMin;
    return sqrt(xr*xr+yr*yr+zr*zr)/2.0;
}

double PlotableObject3D::xCenter() const{
    return (_xMin+_xMax)/2.0;
}

double PlotableObject3D::yCenter() const{
    return (_yMin+_yMax)/2.0;
}

double PlotableObject3D::zCenter() const{
    return (_zMin+_zMax)/2.0;
}

void PlotableObject3D::setColor(const QColor& color){
    _color = color;
    emit changed();
}

void PlotableObject3D::reset(){
    _id = 0;
}

void PlotableObject3D::cleanUp(){
    _xMin = Real::positiveInfinity();
    _yMin = Real::positiveInfinity();
    _zMin = Real::positiveInfinity();
    _xMax = Real::negativeInfinity();
    _yMax = Real::negativeInfinity();
    _zMax = Real::negativeInfinity();
}
